package com.hxht.storage.server.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;

import com.hxht.storage.server.dao.PlanRecordDao;
import com.hxht.storage.server.pojo.PlanRecordInfo;
import com.hxht.storage.server.pojo.VideoManualRecord;

@Repository
public class PlanRecordDaoImpl extends BaseDaoImpl<PlanRecordInfo> implements
		PlanRecordDao {

	@PostConstruct
	@Override
	public void init() {
		// TODO Auto-generated method stub
		clazz = PlanRecordInfo.class;
	}

	@Override
	public List<PlanRecordInfo> getRecordByDevice(String deviceId,
			int channelIndex, Date start, Date stop) {
		// TODO Auto-generated method stub
		List<PlanRecordInfo> recordList = new ArrayList<PlanRecordInfo>();

		StringBuilder queryStr = new StringBuilder("from PlanRecordInfo where deviceId=:deviceId and deviceChannel=:channel");

		// 只查找 录像时间片段 和 搜索时间段 有重复的 录像记录
		if (start != null) {
			queryStr.append(" and not ( startTime >= :stop or (stopTime != null and stopTime <= :start) )");
		}

		TypedQuery<PlanRecordInfo> query = entityManage
				.createQuery(queryStr.toString(), clazz);
		query.setParameter("deviceId", deviceId).setParameter("channel",
				channelIndex);

		if (start != null) {
			query.setParameter("start", start);
			query.setParameter("stop", stop);
		}

		recordList = query.getResultList();

		return recordList;
	}

	@Override
	public PlanRecordInfo getRecodingDevice(String deviceId, int channelIndex) {
		// TODO Auto-generated method stub
		List<PlanRecordInfo> recordList;
		
		TypedQuery<PlanRecordInfo> query = entityManage.createQuery(
				"from PlanRecordInfo where deviceId=:deviceId and deviceChannel=:channel and isRecoding=:recording", clazz)
				.setParameter("deviceId", deviceId).setParameter("channel", channelIndex).setParameter("recording", true);
		
		recordList = query.getResultList();
		
		if (recordList != null && recordList.size() != 0){
			return recordList.get(0);
		} else {
			return null;
		}
	}

	@Override
	public PlanRecordInfo getRecodingByIpchannel(String nvrIp, int ipchannel) {
		
		List<PlanRecordInfo> recordingList;
		
		TypedQuery<PlanRecordInfo> query = entityManage.createQuery(
				"select record from PlanRecordInfo as record left join record.ipChannel as ipchannel"
				+ " left join ipchannel.nvrDevice as nvr where nvr.ip=:nvrIp and "
				+ "ipchannel.dvrChannel=:channel and record.isRecoding=:recording", clazz)
				.setParameter("nvrIp", nvrIp).setParameter("channel", ipchannel).setParameter("recording", true);
		
		recordingList = query.getResultList();
		
		if (recordingList != null && recordingList.size() != 0){
			return recordingList.get(0);
		} else {
			return null;
		}
	}

	@Override
	public PlanRecordInfo getRecordByUuid(String recordUuid) {

		PlanRecordInfo record = null;

		TypedQuery<PlanRecordInfo> query = entityManage.createQuery(
				"from PlanRecordInfo where uuid=:uuid", clazz).setParameter(
				"uuid", recordUuid);

		List<PlanRecordInfo> recordList = query.getResultList();

		if (recordList != null && recordList.size() != 0) {
			record = recordList.get(0);
		}

		return record;
	}

	@Override
	public List<PlanRecordInfo> getRecodingDevice() {
		// TODO Auto-generated method stub
		List<PlanRecordInfo> recordList = null;
		
		TypedQuery<PlanRecordInfo> query = entityManage.createQuery(
				"from PlanRecordInfo where isRecoding=:recording", clazz)
				.setParameter("recording", true);
		
		recordList = query.getResultList();
		
		return recordList;
	}

}
